package com.aerors.DataProcess;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.GeoTools;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;

public class TestGeotoolsQuery {
	private static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());
	private static final String shpPath = "C:\\data\\shapes\\sjy4326.shp";
	private static final String fid = "sjy4326.113";
	
	public static void main(String[] args) throws IOException {
		DataStore dataStore = null;
		try {
			File file = new File(shpPath);
			Map<String, Object> map = new HashMap<>();
			map.put("url", file.toURI().toURL());
			dataStore = DataStoreFinder.getDataStore(map);
			String typeName = dataStore.getTypeNames()[0];
			FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);

			Filter idfilter = ff.id(ff.featureId(fid));

			Query query = new Query(typeName, idfilter, new String[] {});
			FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = source.getFeatures(query);
			FeatureIterator<SimpleFeature> iterator = featureCollection.features();
			while (iterator.hasNext()) {
				SimpleFeature feature = iterator.next();

				System.out.println(feature.getID());
				for(Property p : feature.getProperties()) {
					System.out.println(p.getName().toString() + ":" + p.getValue());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dataStore.dispose();
		}
	}
}
